package com.laihuichuxing.order.concurrent;
 /**
  * Description: 线程池内线程自启
  * Author: Qinxuefeng
  * Version: 1.0
  * Create Date Time: 2018/10/15 17:26.
  * Update Date Time: 
  */
public class InternalTask implements Runnable{

	private final RunnableQueue runnableQueue;
	
	private volatile boolean running = true;
	
	public InternalTask(RunnableQueue runnableQueue) {
		
		this.runnableQueue = runnableQueue;
	}
	
	@Override
	public void run() {

		//如果当前任务为running，并且没有被中断，则不断的从queue中取对象runnable,然后执行run
		while(running&&!Thread.currentThread().isInterrupted()) {
			try {
				Runnable task = runnableQueue.take();
				task.run();
			}catch (Exception e) {
				this.running = false;
				break;
			}
		}
	}

	public void stop() {
		this.running = false;
	}
}
